package com.lczyfz.campusinfo.modules.thirdpay.web;

import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.common.enums.OrderType;
import com.lczyfz.campusinfo.common.enums.PayType;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoAbroad;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoPubmed;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoAbroadService;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoPubmedService;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoExam;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoServant;
import com.lczyfz.campusinfo.modules.campusinfoexam.entity.CampusinfoTrain;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoExamService;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoServantService;
import com.lczyfz.campusinfo.modules.campusinfoexam.service.CampusinfoTrainService;
import com.lczyfz.campusinfo.modules.campusinfopay.entity.CampusinfoPay;
import com.lczyfz.campusinfo.modules.campusinfopay.service.CampusinfoPayService;
import com.lczyfz.campusinfo.modules.thirdpay.wxpay.WXPayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by maple on 2017-07-15.
 */
@Controller
@RequestMapping(value = "i/wxpay")
public class WxpayController extends BaseController {
    @Autowired
    private CampusinfoPayService campusinfoPayService;
    @Autowired
    private CampusinfoAbroadService campusinfoAbroadService; //出国
    @Autowired
    private CampusinfoPubmedService campusinfoPubmedService;//考研
    @Autowired
    private CampusinfoServantService campusinfoServantService;//公务员
    @Autowired
    private CampusinfoExamService campusinfoExamService; //模拟
    @Autowired
    private CampusinfoTrainService campusinfoTrainService; //考证
    @RequestMapping(value = "wxPayCallBack")
    @ResponseBody
    public String notify(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        try {
            String xml = WXPayUtil.getNotifyResult(request);
            Map<String, String> notifyMap = WXPayUtil.xmlToMap(xml);//将微信发的xml转map
                if(notifyMap.get("result_code").equals("SUCCESS")){
                    String outTradeNo = notifyMap.get("out_trade_no");//商户订单号
                    String amountpaid = notifyMap.get("total_fee");//实际支付的订单金额:单位 分
                    double money =Integer.parseInt(amountpaid)/100;
                    CampusinfoPay campusinfoPay = campusinfoPayService.get(outTradeNo);
			        campusinfoPay.setId(outTradeNo);
			        campusinfoPay.setPaytype(PayType.WECHAT.getValue());
     		        campusinfoPay.setStat(CampusinfoOrderStat.DONE.getValue());
     		        campusinfoPay.setAmount(""+money);
     		        campusinfoPayService.save(campusinfoPay);
                    if(outTradeNo.startsWith(OrderType.Servan.getValue())){ //公务员开头
                        CampusinfoServant findcampusinfoServant = new CampusinfoServant();
                        findcampusinfoServant.setOrderCode(outTradeNo);
                        CampusinfoServant campusinfoServant =campusinfoServantService.getOrderCode(findcampusinfoServant);
                        campusinfoServant.setStatus(CampusinfoOrderStat.DONE.getValue());
                        campusinfoServantService.status(campusinfoServant);
                    }else if(outTradeNo.startsWith(OrderType.Train.getValue())) { //考证
                        CampusinfoTrain findcampusinfoTrain = new CampusinfoTrain();
                        findcampusinfoTrain.setOrderCode(outTradeNo);
                        CampusinfoTrain campusinfoTrain = campusinfoTrainService.getOrderCode(findcampusinfoTrain);
                        campusinfoTrain.setStatus(CampusinfoOrderStat.DONE.getValue());
                        campusinfoTrainService.status(campusinfoTrain);
                    } else if(outTradeNo.startsWith(OrderType.Exam.getValue())){ //考试
                        CampusinfoExam findcampusinfoExam = new CampusinfoExam();
                        findcampusinfoExam.setOrderCode(outTradeNo);
                        CampusinfoExam campusinfoExam = campusinfoExamService.getOrderCode(findcampusinfoExam);
                        campusinfoExam.setStatus(CampusinfoOrderStat.DONE.getValue());
                        campusinfoExamService.status(campusinfoExam);
                    } else  if(outTradeNo.startsWith(OrderType.Abroad.getValue())) {//出国{
                        CampusinfoAbroad findcampusinfoAbroad = new CampusinfoAbroad();
                        findcampusinfoAbroad.setOrderCode(outTradeNo);
                        CampusinfoAbroad campusinfoAbroad= campusinfoAbroadService.getOrderCode(findcampusinfoAbroad);
                        campusinfoAbroad.setStatus(CampusinfoOrderStat.DONE.getValue());
                        campusinfoAbroadService.status(campusinfoAbroad);
                    } else if(outTradeNo.startsWith(OrderType.Major.getValue())){
                        CampusinfoPubmed findcampusinfoPumbe = new CampusinfoPubmed();
                        findcampusinfoPumbe.setOrderCode(outTradeNo);
                        CampusinfoPubmed campusinfoPubmed = campusinfoPubmedService.getOrderCode(findcampusinfoPumbe);
                        campusinfoPubmed.setStatus(CampusinfoOrderStat.DONE.getValue());
                        campusinfoPubmedService.status(campusinfoPubmed);
                    }
                    Map<String, Object> payInfo = new HashMap<>();
                    payInfo.put("amount",  money);
                    model.addAttribute("orderNo",outTradeNo);
                    model.addAttribute("payInfo", payInfo);
			        return "success";
                }else if(notifyMap.get("result_code").equals("FAIL")){
                        String outTradeNo = notifyMap.get("out_trade_no");//商户订单号
                        logger.info("验签失败:"+outTradeNo);
                        CampusinfoPay campusinfoPay = new CampusinfoPay();
                        campusinfoPay.setId(outTradeNo);
                        campusinfoPay.setPaytype(PayType.ALIPAY.getValue());
                        campusinfoPay.setStat(CampusinfoOrderStat.FAIL.getValue());
                        campusinfoPayService.save(campusinfoPay);
                        return "fail";
                }
            //告诉微信服务器收到信息了，不要在调用回调action了========这里很重要回复微信服务器信息用流发送一个xml即可
            response.getWriter().write("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
